<?php
// +----------------------------------------------------------------------
// | Azthink通用开发平台 [ TOMORROW IS ANOTHER DAY ]
// +----------------------------------------------------------------------
// | Copyright (c) 2018-2019 http://www.azthink.top All rights reserved.
// +----------------------------------------------------------------------
// | Author 李忠
// +----------------------------------------------------------------------

namespace app\edu\model;

use think\Model;

/**
 * 班级模型
 * @author 李忠
 * @createAt 2018-11-08 14:35:59
 */
class Clazz extends Model {
    // 设置当前模型对应的完整数据表名称
    protected $table = '__EDU_CLAZZ__';

    // 自动写入时间戳
    protected $autoWriteTimestamp = true;

    //数据自动完成
    protected $auto = ['update_by' => UID];
    protected $insert = ['create_by' => UID];

    //修改器
    public function getStatusTextAttr($value, $data) {
        return get_status_html($data['status']);
    }

    //自定义方法
    public static function lists($status, $office_id, $major_name, $title) {
        $map = array();
        if ($status !== null && $status !== '') {
            array_push($map, ['a.status', '=', $status]);
        } else {
            array_push($map, ['a.status', '>=', 0]);
        }

        if ($office_id !== '' && !empty($office_id)) {
            array_push($map, ['b.office_id', '=', $office_id]);
        }
        if ($major_name !== '' && !empty($major_name)) {
            array_push($map, ['a.major_name', '=', $major_name]);
        }

        if ($title !== '' && !empty($title)) {
            array_push($map, ['a.title', 'like', '%' . $title . '%']);
        }
        return self::alias('a')
            ->join('__EDU_MAJOR__ b', 'a.major_name=b.name')
            ->join('__SYS_OFFICE__ c', 'b.office_id=c.id')
            ->where($map)
            ->field('a.*, b.title as major_text, c.title as office_text')
            ->order('c.id,b.name,a.grade,a.name')->paginate();
    }

    public static function state($id, $status) {
        return self::where('id', $id)->update(['status' => $status]);
    }
}
